//
//  TaskManager.m
//  gong
//
//  Created by echo on 14-1-13.
//  Copyright (c) 2014年 echo. All rights reserved.
//

#import "TaskManager.h"
#import "Constants.h"
#import "GameManager.h"

@implementation TaskManager
{
    NSInteger _currentTaskIndex;
    NSArray *_taskNameList;
}

SharedInstanceBuilder(TaskManager)

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (id)init
{
    self = [super init];
    if (self) {
        NSString *fullpath = [[NSBundle mainBundle] pathForResource:@"任务列表" ofType:@"txt" inDirectory:@"Scripts"];
        NSString *scriptContent = [NSString stringWithContentsOfFile:fullpath encoding:NSUTF8StringEncoding error:NULL];
        _taskNameList = [scriptContent componentsSeparatedByString:@"\n"];
        
        [self initTaskStatus];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(whenTaskIsDone:) name:MsgTaskIsDone object:nil];
    }
    return self;
}

- (void)initTaskStatus
{
    [self clearCurrentTask];
    _currentTaskIndex = [[NSUserDefaults standardUserDefaults] integerForKey:TaskIndexKey];
    _currentTask = [self getCurrentTask];
}

- (Task*)getCurrentTask
{
    if (_currentTask) {
        return _currentTask;
    } else {
        _currentTask = [self getTaskByIndex:_currentTaskIndex];
        return _currentTask;
    }
}

- (void)clearCurrentTask
{
    _currentTask = nil;
}

- (Task*)getNextTask
{
    NSInteger newIndex = _currentTaskIndex + 1;
    if (newIndex < _taskNameList.count) {
        _currentTaskIndex = newIndex;
//        [[NSUserDefaults standardUserDefaults] setInteger:_currentTaskIndex forKey:TaskIndexKey];
        
        _currentTask = [self getTaskByIndex:_currentTaskIndex];
    } else {
        _currentTask = nil;
    }
    return _currentTask;
}

- (BOOL)isLastTask
{
    return [_taskNameList indexOfObject:_currentTask.name] == _taskNameList.count - 1;
}

- (Task*)getTaskByIndex:(NSInteger)index
{
    Task *task;
    if (index < _taskNameList.count) {
        NSString *taskName = _taskNameList[index];
        
        NSString *fullpath = [[NSBundle mainBundle] pathForResource:taskName ofType:@"task" inDirectory:@"Scripts"];
        NSData *data = [NSData dataWithContentsOfFile:fullpath];
        if (data) {
            task = [self getTaskByScript:data];
            [GameManager sharedInstance].memory.taskIndex = _currentTaskIndex;
        }
    }
    return task;
}

- (Task*)getTaskByScript:(NSData*)data
{
    Task *task;
    
    id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    if ([json isKindOfClass:[NSDictionary class]]) {
        task = [[Task alloc] init];
        task.name = json[@"名称"];
        task.description = json[@"描述"];
        task.triggerPlace = json[@"场所"];
        task.scriptName = json[@"情景"];
        task.awardJade = [json[@"奖励"][@"玉石"] integerValue];
    }
    
    return task;
}

- (NSString*)getCurrentScript
{
    return _currentTask.scriptName;
}
@end
